import Dexie, { Table } from 'dexie';
import {NumberIdAndName} from "../types/BasicModels";
import {SubGoodsResponse} from "../apis/GoodsApi";
import {OffsetDateTime} from "@js-joda/core";

export interface GoodsScratch {
    id?: number;
    createdDateTime:string
}

export interface GoodsScratchItem{
    itemId?: number
    name: string
    amount: number
    price: number
    barcode: string
    createdDateTime: string
    lastModifiedDateTime: string
    brand?: NumberIdAndName
    category?: NumberIdAndName
    factory?: NumberIdAndName
    boxSpecification?:string
    id:number
    scratchId:number
    subGoods?:SubGoodsResponse
}

export interface GoodsAmountAndUnitUseHistory{
    id?:number
    goodsId:number
    unitId?:number
    price?:number
    amount:number
    scope: GoodsAmountAndUnitUseHistoryScope
    resourceId?:number
}

export enum GoodsAmountAndUnitUseHistoryScope{
    MARKET_ORDER,
    PROCUREMENT,
    PROCUREMENT_RETURN,
}

export class MyDatabase extends Dexie {
    // 'friends' is added by dexie when declaring the stores()
    // We just tell the typing system this is the case
    goodsScratch!: Table<GoodsScratch>;

    goodsScratchItem!: Table<GoodsScratchItem>;

    goodsAmountAndUnitUseHistory!: Table<GoodsAmountAndUnitUseHistory>;

    constructor() {
        super('myDatabase');
        // TODO add subGoods
        this.version(1).stores({
            goodsScratch: '++id',
            goodsScratchItem: '++itemId, name, barcode, scratchId',
            goodsAmountAndUnitUseHistory:"id++, [scope+resourceId], unitId, amount, goodsId"
        });
    }
}

export const myDatabase = new MyDatabase();
